package com.example.demo.controller;

import com.example.demo.bean.ErrorCode;
import com.example.demo.bean.JsonData;
import com.example.demo.bean.ThisSystemException;
import com.example.demo.repository.Open;
import com.example.demo.table.UserTable;
import com.example.demo.utli.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class MyHandlerInterceptor implements HandlerInterceptor {

    @Autowired
    RedisUtil mRedisUtil;

    /**
     * 单个实现类的执行顺序
     * preHandler -> Controller -> postHandler -> model渲染-> afterCompletion
     * **/
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {

        /*该方法将在请求处理之前进行调用，只有该方法返回true，才会继续执行后续的Interceptor和Controller，
        当返回值为true 时就会继续调用下一个Interceptor的preHandle 方法，
        如果已经是最后一个Interceptor的时候就会是调用当前请求的Controller方法；*/
        System.out.println("url" + httpServletRequest.getRequestURI());
        if ("OPTIONS".equalsIgnoreCase(httpServletRequest.getMethod())) {
            return true;
        }

        System.out.println(httpServletRequest.getSession().getId());

        HandlerMethod handlerMethod ;
        if (o instanceof HandlerMethod) {
            handlerMethod = (HandlerMethod) o;
            Open methodAnnotation = handlerMethod.getMethodAnnotation(Open.class);
            boolean b = handlerMethod.hasMethodAnnotation(Open.class);
            if (b) {
                return true;
            }
        }



//        Object obj =  mRedisUtil.get(httpServletRequest.getSession().getId());
//        String token = httpServletRequest.getHeader("token");
//        Object obj =  mRedisUtil.get(token);
//        if (null == obj) {
//            httpServletResponse.setHeader("content-type", "application/json;charset=UTF-8");
//
//            JsonData fail = JsonData.fail(ErrorCode.PLEASE_LOG_IN_FIRST.getStatus(), String.valueOf(ErrorCode.PLEASE_LOG_IN_FIRST.getText()));
//            String s = fail.toString();
//            httpServletResponse.getWriter().println(s);
//            return false;
//            //不知道咋回事，可能抛出异常后其他地方还在执行逻辑，导致前端无法获取到数据
////            throw new ThisSystemException(ErrorCode.PLEASE_LOG_IN_FIRST.getText(),ErrorCode.PLEASE_LOG_IN_FIRST.getStatus());
//        }
//        UserTable user = (UserTable) obj;
//        if (null == httpServletRequest.getSession(false)){
//            throw new ThisSystemException("登录失效",20);
//        }

//        HttpSession session = httpServletRequest.getSession();
//        session.setMaxInactiveInterval(1);

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        /*该方法也是需要当前对应的Interceptor的preHandle方法的返回值为true时才会执行，
        该方法将在整个请求结束之后，也就是在DispatcherServlet 渲染了对应的视图之后执行。
        用于进行资源清理。*/

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        /*该方法将在请求处理之后，DispatcherServlet进行视图返回渲染之前进行调用，
        可以在这个方法中对Controller 处理之后的ModelAndView 对象进行操作。*/
    }




}
